<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleEdit()" v-action:addAdminUser>新建</a-button>
      </div>

      <s-table ref="table" size="default" rowKey="id" :columns="columns" :data="loadData" showPagination="auto">
        <!-- <span slot="roles" slot-scope="text, record, index"> -->
        <span slot="roles" slot-scope="text">
          <a-tag color="red" v-for="item in text" :key="item">{{ item }}</a-tag>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a-button style="margin-right: 8px;" @click="handleEdit(record)" v-action:updateAdminUser>编辑</a-button>
            <a-button type="primary" @click="editRole(record)">分配角色</a-button>
          </template>
        </span>
      </s-table>
    </a-card>
    <a-modal title="Title" :visible="editVisible" width="40%" @ok="handleSubmit" @cancel="editVisible = false">
      <a-form-model :model="editForm" :labelCol="{ span: 6 }" :wrapperCol="{ span: 14 }">
        <a-form-model-item
          label="管理员账号"
          prop="username"
          :rules="{ required: true, message: '请输入管理员账号', trigger: 'blur' }"
        >
          <a-input v-model="editForm.username" />
        </a-form-model-item>
        <a-form-model-item
          label="管理员昵称"
          prop="nickname"
          :rules="{ required: true, message: '请输入管理员昵称', trigger: 'blur' }"
        >
          <a-input v-model="editForm.nickname" />
        </a-form-model-item>
        <a-form-model-item label="管理员密码">
          <a-input v-model="editForm.password" />
        </a-form-model-item>
        <a-form-model-item label="确认密码">
          <a-input v-model="editForm.confirm_password" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>

    <a-modal title="分配角色" :visible="roleVisible" @ok="handleEditRoles" @cancel="roleVisible = false">
      <a-checkbox-group :defaultValue="selectRoles" @change="onChangeRoles">
        <a-checkbox :value="role.id" v-for="role in roleList" :key="role.id">
          {{ role.name }}
        </a-checkbox>
      </a-checkbox-group>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { AdminUserList, AdminUserAdd, AdminUserUpdate, AdminUserDestory, SimpleRoles, AllotRoles } from '@/api/admin'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: '6%',
    sorter: true,
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '用户名',
    dataIndex: 'username',
    width: '10%'
  },
  {
    title: '昵称',
    dataIndex: 'nickname',
    width: '10%'
  },
  {
    title: '角色',
    dataIndex: 'roles',
    width: '20%',
    scopedSlots: { customRender: 'roles' }
  },
  {
    title: '最后登录时间',
    dataIndex: 'last_login_time'
  },
  {
    title: '更新时间',
    dataIndex: 'updated_at'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '200px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis
  },
  data() {
    this.columns = columns
    return {
      // 查询参数
      queryParam: {},
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return AdminUserList(requestParameters).then(res => {
          console.log(res.data)
          return res.data
        })
      },
      roleList: [],
      editVisible: false,
      roleVisible: false,
      editForm: {},
      selectRoles: []
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    getRoles() {
      SimpleRoles().then(res => {
        this.roleList = res.data
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm() {
      this.queryParam = {}
      this.$refs.table.refresh(true)
    },
    handleEdit(row) {
      this.editVisible = true
      this.editForm = Object.assign({}, row)
    },
    handleSubmit() {
      if (this.editForm.id) {
        AdminUserUpdate(this.editForm.id, this.editForm).then(res => this.success(res))
      } else {
        AdminUserAdd(this.editForm).then(res => this.success(res))
      }
    },
    success(res) {
      this.editVisible = false
      this.roleVisible = false
      this.$notification.success({ message: res.msg })
      this.$refs.table.refresh(true)
    },
    editRole(row) {
      this.roleVisible = true
      this.adminUser = row.id
      this.roleList.forEach(e => {
        if (row.roles.includes(e.name)) {
          this.selectRoles.push(e.id)
        }
      })
    },
    onChangeRoles(checkedValues) {
      this.selectRoles = checkedValues
    },
    handleEditRoles() {
      AllotRoles(this.adminUser, { roles: this.selectRoles }).then(res => this.success(res))
    }
  }
}
</script>
